package net.shenru.ui;

import java.util.ArrayList;
import java.util.List;

import net.shenru.BaseActivity;
import net.shenru.NormalAdatper;
import net.shenru.R;
import net.shenru.modle.Navigate;
import net.shenru.os.utlis.SrToast;
import net.shenru.ui.encog.EncogBusActivity;
import net.shenru.ui.map.BaiduMapActivity;
import net.shenru.ui.zxing.CaptureActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;


public class NavigateActivity extends BaseActivity implements OnItemClickListener {

	private GridView mNavigateView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.navigate_activity);

		findView();

		NavigateAdapter adapter = new NavigateAdapter(this, getData());
		mNavigateView.setAdapter(adapter);

		mNavigateView.setOnItemClickListener(this);
	}

	// 导航
	private List<Navigate> getData() {
		ArrayList<Navigate> list = new ArrayList<Navigate>();
		Intent mapIntent = new Intent(NavigateActivity.this, BaiduMapActivity.class);
		Intent zxingIntent = new Intent(NavigateActivity.this , CaptureActivity.class);

		list.add(new Navigate("我的地图", getResources().getDrawable(R.drawable.nav_map), mapIntent));
		list.add(new Navigate("我的英语", getResources().getDrawable(R.drawable.nav_english), zxingIntent));
		list.add(new Navigate("我的管家", getResources().getDrawable(R.drawable.nav_butle), null));
		list.add(new Navigate("我的世界", getResources().getDrawable(R.drawable.nav_word), null));
		return list;
	}

	// 事件
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Navigate navigate = (Navigate) mNavigateView.getAdapter().getItem(position);
		Intent it = navigate.getIntent();
		if(it != null){
			startActivity(navigate.getIntent());
		}else{
			SrToast.show(getApplicationContext(), "还没有竣工");
		}
	}

	private void findView() {
		mNavigateView = (GridView) findViewById(R.id.grdv);
	}

	private class NavigateAdapter extends NormalAdatper<Navigate> {

		// 泛型。我的了解为不缺定的以知类型
		public NavigateAdapter(Context context, List<Navigate> data) {
			super(context, data);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Navigate nav = getItem(position);
			View view = View.inflate(mContext, R.layout.navigate_item, null);
			ImageView iv = (ImageView) view.findViewById(R.id.img_navigate);
			iv.setImageDrawable(nav.getDrawable());
			return view;
		}

	}

}
